﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Speech.Synthesis;
using BUS;
using DTO;

namespace Support_Study_Vocabulary
{
    public partial class frmReviewWordSetting : Office2007Form
    {
        public SpeechSynthesizer _speaker;
        public frmReviewWordSetting()
        {
            InitializeComponent();
        }

        private void chbTestAll_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chbTestAll.Checked == true)
            {
                this.groupBox1.Enabled = false;
                this.chbFilter.Enabled = false;
                this.cbTopic.Enabled = false;
            }
            else
            {
                this.groupBox1.Enabled = true;
                this.chbFilter.Enabled = true;
                this.cbTopic.Enabled = true;
            }
        }

        private void chbNgay_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chbNgay.Checked == true)
            {
                this.chbThang.Enabled = false;
                this.dpkThang.Enabled = false;
                this.chbNgauNhien.Enabled = false;
                this.txtSoTu.Enabled = false;
            }
            else
            {
                this.chbThang.Enabled = true;
                this.dpkThang.Enabled = true;
                this.chbNgauNhien.Enabled = true;
                this.txtSoTu.Enabled = true;
            }
        }

        private void chbThang_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chbThang.Checked == true)
            {
                this.chbNgay.Enabled = false;
                this.dpkNgayBD.Enabled = false;
                this.dpkNgayKT.Enabled = false;
                this.chbNgauNhien.Enabled = false;
                this.txtSoTu.Enabled = false;
            }
            else
            {
                this.chbNgay.Enabled = true;
                this.dpkNgayBD.Enabled = true;
                this.dpkNgayKT.Enabled = true;
                this.chbNgauNhien.Enabled = true;
                this.txtSoTu.Enabled = true;
            }
        }

        private void chbNgauNhien_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chbNgauNhien.Checked == true)
            {
                this.chbNgay.Enabled = false;
                this.dpkNgayBD.Enabled = false;
                this.dpkNgayKT.Enabled = false;
                this.chbThang.Enabled = false;
                this.dpkThang.Enabled = false;
            }
            else
            {
                this.chbNgay.Enabled = true;
                this.dpkNgayBD.Enabled = true;
                this.dpkNgayKT.Enabled = true;
                this.chbThang.Enabled = true;
                this.dpkThang.Enabled = true;
            }
        }

        private void chbFilter_CheckedChanged(object sender, EventArgs e)
        {
            if (chbFilter.Checked == true)
            {
                cbTopic.Visible = true;
            }
            else
            {
                cbTopic.Visible = false;
            }
        }

        private void frmReviewWordSetting_Load(object sender, EventArgs e)
        {
            this.cbTypeTest.SelectedIndex = 0;
            List<TOPIC> lsTopic = TopicBUS.getAllTopics();
            this.cbTopic.DisplayMember = "Name";
            this.cbTopic.ValueMember = "idTopic";
            this.cbTopic.DataSource = lsTopic;
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        protected List<VOCABULARY> CreateReviewWords()
        {
            List<VOCABULARY> lsRes = null;
            if (rbtnTestAll.Checked)
            {
                #region All type
                if (this.chbTestAll.Checked == true)
                {
                    lsRes = VocabularyBUS.getListVocabularies();
                }
                else
                {
                    int idTopic = -1;
                    if (this.chbFilter.Checked == true)
                    {
                        idTopic = int.Parse(this.cbTopic.SelectedValue.ToString());
                    }

                    if (this.chbNgay.Checked == true)
                    {
                        if (idTopic != -1)
                        {
                            lsRes = VocabularyBUS.getListVocabulariesbyDateToDate(this.dpkNgayBD.Value, this.dpkNgayKT.Value, idTopic);
                        }
                        else
                        {
                            lsRes = VocabularyBUS.getListVocabulariesbyDateToDate(this.dpkNgayBD.Value, this.dpkNgayKT.Value);
                        }
                    }
                    else
                    {
                        if (this.chbThang.Checked == true)
                        {
                            if (idTopic != -1)
                            {
                                lsRes = VocabularyBUS.getListVocabulariesbyMonth(this.dpkThang.Value, idTopic);
                            }
                            else
                            {
                                lsRes = VocabularyBUS.getListVocabulariesbyMonth(this.dpkThang.Value);
                            }
                        }
                        else
                        {
                            if (this.chbNgauNhien.Checked == true)
                            {
                                int sotu;
                                if (int.TryParse(this.txtSoTu.Text, out sotu))
                                {
                                    if (idTopic != -1)
                                    {
                                        lsRes = VocabularyBUS.getListRandVocabularies(sotu, idTopic);
                                    }
                                    else
                                    {
                                        lsRes = VocabularyBUS.getListRandVocabularies(sotu);
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("Hãy nhập số từ!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                }
                            }
                            else
                            {
                                MessageBox.Show("Hãy chọn một trong 3 kiểu trên!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                        }
                    }
                }
                #endregion
            }
            else
            {
                #region Is two word verb or not
                short isTwoWordVerb = 0; //0 ~ false;
                if (rbtnTestOnlyTwoWord.Checked)
                {
                    isTwoWordVerb = 1;
                }                
                if (this.chbTestAll.Checked == true)
                {
                    lsRes = VocabularyBUS.getListVocabularies(isTwoWordVerb);
                }
                else
                {
                    int idTopic = -1;
                    if (this.chbFilter.Checked == true)
                    {
                        idTopic = int.Parse(this.cbTopic.SelectedValue.ToString());
                    }

                    if (this.chbNgay.Checked == true)
                    {
                        if (idTopic != -1)
                        {
                            lsRes = VocabularyBUS.getListVocabulariesbyDateToDate(this.dpkNgayBD.Value, this.dpkNgayKT.Value, idTopic, isTwoWordVerb);
                        }
                        else
                        {
                            lsRes = VocabularyBUS.getListVocabulariesbyDateToDate(this.dpkNgayBD.Value, this.dpkNgayKT.Value, isTwoWordVerb);
                        }
                    }
                    else
                    {
                        if (this.chbThang.Checked == true)
                        {
                            if (idTopic != -1)
                            {
                                lsRes = VocabularyBUS.getListVocabulariesbyMonth(this.dpkThang.Value, idTopic, isTwoWordVerb);
                            }
                            else
                            {
                                lsRes = VocabularyBUS.getListVocabulariesbyMonth(this.dpkThang.Value, isTwoWordVerb);
                            }
                        }
                        else
                        {
                            if (this.chbNgauNhien.Checked == true)
                            {
                                int sotu;
                                if (int.TryParse(this.txtSoTu.Text, out sotu))
                                {
                                    if (idTopic != -1)
                                    {
                                        lsRes = VocabularyBUS.getListRandVocabularies(sotu, idTopic, isTwoWordVerb);
                                    }
                                    else
                                    {
                                        lsRes = VocabularyBUS.getListRandVocabularies(sotu, isTwoWordVerb);
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("Hãy nhập số từ!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                }
                            }
                            else
                            {
                                MessageBox.Show("Hãy chọn một trong 3 kiểu trên!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                        }
                    }
                }
                #endregion
            }
            return lsRes;            
        }        

        protected void MixVocabularies(ref List<VOCABULARY> source)
        {
            Random iRn = new Random();

            for (int i = 0; i < source.Count; i++)
            {
                int ranIndex = iRn.Next(source.Count - 1);
                VOCABULARY temp = source[i];                
                source[i] = source[ranIndex];
                source[ranIndex] = temp;
            }
        }

        private void btnSave_Close_Click(object sender, EventArgs e)
        {
            List<VOCABULARY> lsReview = this.CreateReviewWords();
            if (lsReview != null && lsReview.Count > 0)
            {
                MixVocabularies(ref lsReview);
                frmTestVocabulary frm = new frmTestVocabulary();
                frm.LsVcSource = lsReview;
                frm.MdiParent = this.MdiParent;
                frm._speaker = _speaker;
                frm.typeReview = cbTypeTest.SelectedIndex;
                frm.DrawCtrlTestVocabulary();
                frm.Show();
            }
            else
            {
                MessageBox.Show("Không có từ nào để ôn.!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }
}
